package com.sc.javaee.controller;

import com.sc.javaee.pojo.Products;
import com.sc.javaee.pojo.Result;
import com.sc.javaee.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProductsController {
    @Autowired
    ProductsService ps;
    @RequestMapping("/getProducts")
    public Result getProducts(Integer categoryId){
        List<Products> list=ps.getProducts(categoryId);
        return new Result(1,"查询成功",list);
    }
    //热门推荐
    @RequestMapping("/getRandomProducts")
    public Result getRandomProducts(){
        List<Products> list=ps.getRandomProducts();
        return new Result(1,"查询成功",list);
    }
    @RequestMapping("/searchProducts")
    public Result searchProducts(Integer categoryId, String keyword) {
        List<Products> list = ps.searchProducts(categoryId, keyword);
        return new Result(1, "搜索成功", list);
    }
    @RequestMapping("/searchAllProducts")
    public Result searchAllProducts(String keyword) {
        List<Products> list = ps.searchAllProducts(keyword);
        return new Result(1, "搜索成功", list);
    }
    @RequestMapping("/getProductDetail")
    public Result getProductDetail(Integer id) {
        Products product = ps.getProductDetail(id);
        if (product != null) {
            return new Result(1, "查询成功", product);
        } else {
            return new Result(0, "商品不存在", null);
        }
    }
}
